package com.jml.jedis;


import java.util.HashSet;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author:jml
 * @date: 2020/3/2 16:07
 * @description：jedis默认是不支持集群的，因为不支持重定向,下面这个是支持集群的
 * @在这个里面我们只需要连接一个就可以了不需要吧所有的节点都写入进去
 * @原理就是先查一下key对应的卡槽的位置通过命令 cluster KETSLOT key
 * @cluster nodes 查看这个卡槽在哪个节点上，再去连接这个节点获取value值
 */
public class JedisClusterTest {
	
	private  static JedisCluster jedis;
	
	static {
		Set<HostAndPort> hostAndPortSet = new HashSet<HostAndPort>();
		//hostAndPortSet.add(new HostAndPort("172.16.5.122",7000));
		//hostAndPortSet.add(new HostAndPort("172.16.5.122",7001));
		hostAndPortSet.add(new HostAndPort("172.16.5.122",7002));
		hostAndPortSet.add(new HostAndPort("172.16.5.122",7003));
		hostAndPortSet.add(new HostAndPort("172.16.5.122",7004));
		hostAndPortSet.add(new HostAndPort("172.16.5.122",7005));
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		//最大空闲连接默认8个
		jedisPoolConfig.setMaxIdle(100);
		//最大连接数默认8个
		jedisPoolConfig.setMaxTotal(500);
		//最小空闲连接默认0
		jedisPoolConfig.setMinIdle(0);
		//获取连接最大等待毫秒数
		jedisPoolConfig.setMaxWaitMillis(2000L);
		//对拿到的Connection进行validateObject校验
		jedisPoolConfig.setTestOnBorrow(true);
		jedis = new JedisCluster(hostAndPortSet,jedisPoolConfig);
	}
	
	public static void main(String[] args) {
		JedisClusterTest jedisClusterTest = new JedisClusterTest();
		String mm = jedis.get("mm");
		System.out.println(mm);
	}

}
